Erweitertes Tutorial
In diesem kleinen Tutorial beschreibe ich die Benutzung von SQL-Bindungsvariablen.
Bitte beachten Sie, Bindungsvariablen können in der SQL-Sprache nicht immer oder überall eingesetzt werden!
Okay, los geht es.
Zunächst melden wir uns bei der Datenbank an.
Die Benutzerdaten müssen Sie natürlich Ihren Gegebenheiten anpassen.
@INCLUDE "PostgreSQL_Lib.hws" Block Local Fail, fields, SQL$, i, a Local Errorcode Local Host$ = "localhost" Local Port = 5432 Local Database$ = "postgres" Local Username$ = "test" Local Passwort$ = "test" Local Bind = {} Local Bind1 = {} Local Result = {}
;registration Fail, ErrorCode, ConnectionID = PG:OpenDatabase(Host$, Port, Database$, Username$, Passwort$)
; Evaluating the Return values If Fail = False NPrint("") NPrint("Connection was successfully established.") NPrint("----------------------------------------") Else NPrint("") NPrint("The connection failed.") NPrint("") NPrint("Error code: ", ErrorCode) End EndIf |
Wie bereits im Tutorial zuvor erstellen wir eine einfache Tabelle.
Tabelle Personen:
Vorname |
Typ: text |
Name |
Typ: text |
Alter |
Typ: int4 |
;The SQL query Local SQL$ = "CREATE TABLE Personen (Vorname text, Name text, Alter int4)"
;Create a new table Fail, Errorcode = PG:SimpleUpdate(SQL$, ConnectionID)
; Evaluating the Return values If Fail = False NPrint("") NPrint("Table was created.") NPrint("") NPrint("") Else NPrint("") NPrint("Create Table failed.") NPrint("") NPrint("Error code: ", ErrorCode) EndIf |
Nun wird es zeit die Tabelle mit Daten zu füllen.
Der folgende Code fügt der Tabelle drei Personen hinzu.
An dieser Stelle benutzen wir auch das erste mal die Bindungsariablen.
;Create binding variables InsertItem(Bind, #STRING) ;SQL variable $1 InsertItem(Bind, "Michael") InsertItem(Bind, #STRING) ;SQL variable $2 InsertItem(Bind, "Mustermann") InsertItem(Bind, #INTEGER) ;SQL variable $3 InsertItem(Bind, 34)
InsertItem(Bind, #STRING) ;SQL variable $4 InsertItem(Bind, "Thomas") InsertItem(Bind, #STRING) ;SQL variable $5 InsertItem(Bind, "Mustermann") InsertItem(Bind, #INTEGER) ;SQL variable $6 InsertItem(Bind, 48)
InsertItem(Bind, #STRING) ;SQL variable $7 InsertItem(Bind, "Frank") InsertItem(Bind, #STRING) ;SQL variable $8 InsertItem(Bind, "Testmann") InsertItem(Bind, #INTEGER) ;SQL variable $9 InsertItem(Bind, 27)
;Write data To the table SQL$ = "INSERT INTO Personen VALUES ($1, $2, $3)" Fail, Errorcode = PG:ExtendedUpdate(Bind, SQL$, ConnectionID) If Fail = False NPrint("Database update successful - First person added.") NPrint("") Else NPrint("INSERT INTO people failed.") NPrint("Error code: ", ErrorCode) EndIf SQL$ = "INSERT INTO Personen VALUES ($4, $5, $6)" Fail, Errorcode = PG:ExtendedUpdate(Bind, SQL$, ConnectionID) If Fail = False NPrint("Database update successful - Second person added.") NPrint("") Else NPrint("INSERT INTO people failed.") NPrint("Error code: ", ErrorCode) EndIf SQL$ = "INSERT INTO Personen VALUES ($7, $8, $9)" Fail, Errorcode = PG:ExtendedUpdate(Bind, SQL$, ConnectionID) If Fail = False NPrint("Database update successful - Third person added.") NPrint("") NPrint("") Else NPrint("INSERT INTO people failed.") NPrint("Error code: ", ErrorCode) EndIf |
Die folgenden Code-Zeilen beinhalten eine einfache Suche in der Datenbank.
Wir lassen uns alle Personen anzeigen die 48 Jahre alt sind.
Auch hier benutzen wir wieder eine Bindungsvariable.
;Lists all persons 48 years old. InsertItem(Bind1, #INTEGER) ;SQL variable $1 InsertItem(Bind1, 48)
SQL$ = "SELECT * FROM Personen WHERE Alter = $1" Fail, Errorcode, fields, Result = PG:ExtendedQuery(Bind1, SQL$, ConnectionID)
; Evaluating the Return values If Fail = False NPrint("Find entries with people who are 48 years old.") NPrint("") NPrint("Search result") NPrint("") items = TableItems(Result) For i = 0 To items-1 Step fields For a = 1 To fields Print(Result[i + a-1], " ") Next NPrint("") Next Else NPrint("Query failed.") NPrint("Error code: ", ErrorCode) EndIf |
Als letztes machen wir alles wieder Rückgängig und löschen die Tabelle.
Wenn Sie mit der Tabelle weiter arbeiten möchten, lassen Sie den letzten Schritt einfach aus.
Das gesamte Code-Beispiel finden Sie auch im Archive als Hollywood-Script.
;Delete the table SQL$ = "DROP TABLE Personen;" Fail, Errorcode = PG:SimpleUpdate(SQL$, ConnectionID)
; Evaluating the Return values If Fail = False NPrint("") NPrint("") NPrint("Table has been deleted") Else NPrint("DROP table failed.") NPrint("Error code: ", ErrorCode) EndIf
WaitLeftMouse
;Close connection To the database. PG:CloseDatabase(ConnectionID) EndBlock |
Wenn Sie alles richtig gemacht haben, sollte die Programmausgabe wie folgt aussehen (AmigaOS 3.9):

Mit dem Personal Edition von HelpNDoc erstellt: Hilfedateien für das Qt Help-Framework erstellen